package com.his.modules.business.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.modules.business.dao.CaSynSignDataMapper;
import com.his.modules.business.entity.CaSignData;
import com.his.modules.business.entity.CaSynSignData;
import com.his.modules.business.service.CaSignDataService;
import com.his.modules.business.service.CaSynSignDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

@Service
@Slf4j
public class CaSynSignDataServiceImpl extends ServiceImpl<CaSynSignDataMapper, CaSynSignData> implements CaSynSignDataService {

    @Autowired
    private CaSignDataService caSignDataService;

    @Override
    public void updateCaSynSignDataAndCaSignData(CaSynSignData caSynSignData, JSONObject jsonObject) {
        //更新业务数据为已处理，并记录时间
        caSynSignData.setYwzt(1);
        caSynSignData.setCzsj(LocalDateTime.now());
        this.updateById(caSynSignData);
        //根据CA唯一值获取业务数据并更新签名值及证书值
        if(caSynSignData.getYwlx() == 1) {
            QueryWrapper<CaSignData> caSignDataQueryWrapper = new QueryWrapper<>();
            caSignDataQueryWrapper.eq("CA_ID", caSynSignData.getUniqueid());
            CaSignData caSignData = caSignDataService.getOne(caSignDataQueryWrapper);
            if(!Objects.isNull(caSignData)) {
                caSignData.setQmnr(jsonObject.getStr("p1"));
                caSignData.setCert(jsonObject.getStr("cert"));
                caSignDataService.updateById(caSignData);
            }
        }
    }
}